﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.Security;
using Blog.Data;
using Blog.Models;
using BlogModel;

namespace Blog.Controllers
{
    [Authorize]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            int userId = (int)Session["User"];
            var posts = PostRepository.GetPostsByUserId(userId);
            return View(posts);
        }


        [HttpGet]
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AllowAnonymous]
        public ActionResult Login(UserModel user)
        {
            var authenticatedUser = UserRepository.AuthenticateUser(user.UserName, user.Password);
            if (authenticatedUser != null)
            {
                FormsAuthentication.SetAuthCookie(authenticatedUser.UserName, false);
                Session["User"] = authenticatedUser.UserId;
                return RedirectToAction("Index", new { id = authenticatedUser.UserId });
            }
            else
            {
                ModelState.AddModelError("", "Inloggninguppgifter är inte korrekt!");
            }
            return View(user);
        }

        [HttpGet]
        public ActionResult CreatePost()
        {
            return View(new PostModel());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreatePost(PostModel postModel)
        {
            if (ModelState.IsValid)
            {
                if (postModel.ImageUpload.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(postModel.ImageUpload.FileName);
                    if (fileName != null)
                    {
                        var path = Path.Combine(Server.MapPath("~/Content/images"), fileName);
                        postModel.ImageUpload.SaveAs(path);
                        postModel.FeaturedImage = fileName;
                    }
                }
                var blog = BlogRepository.GetBlogByUserName(User.Identity.Name);
                postModel.BlogId = blog.BlogId;
                postModel.CreationDate = DateTime.UtcNow;

                PostRepository.InsertPost(postModel);
                return RedirectToAction("Index");
            }

            return CreatePost();

        }

        [HttpGet]
        public ActionResult EditPost(int id)
        {
            var postModel = PostRepository.GetPostByPostId(id);
            return View(postModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditPost(int id, PostModel postModel)
        {
            if (ModelState.IsValid)
            {
                if (postModel.ImageUpload.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(postModel.ImageUpload.FileName);
                    if (fileName != null)
                    {
                        var path = Path.Combine(Server.MapPath("~/Content/images"), fileName);
                        postModel.ImageUpload.SaveAs(path);
                        postModel.FeaturedImage = fileName;
                    }
                }

                postModel.PostId = id;
                var blog = BlogRepository.GetBlogByUserName(User.Identity.Name);
                postModel.BlogId = blog.BlogId;
                PostRepository.UpdatePost(postModel);
                ViewBag.Saved = "Inlägget är nu uppdaterat";
                return View(postModel);
            }

            return EditPost(id);

        }

        [HttpGet]
        public ActionResult DeletePost(int id)
        {
            var postModel = PostRepository.GetPostByPostId(id);
            return View(postModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DeletePost(int id, PostModel postModel)
        {
            PostRepository.DeletePost(id);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult BlogRoll()
        {
            var blogRolls = BloggRollRepository.GetAllBlogRolls();
            return View(blogRolls);
        }

        [HttpGet]
        public ActionResult CreateLink()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateLink(BlogRollModel blogRollModel)
        {
            if (ModelState.IsValid)
            {
                var blog = BlogRepository.GetBlogByUserName(User.Identity.Name);
                blogRollModel.BlogId = blog.BlogId;
                BloggRollRepository.InsertBlogRoll(blogRollModel);
                return RedirectToAction("BlogRoll");
            }
            return CreateLink();
        }

        [HttpGet]
        public ActionResult EditLink(int id)
        {
            var blogRoll = BloggRollRepository.GetBlogRollById(id);
            return View(blogRoll);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditLink(int id, BlogRollModel blogRollModel)
        {
            if (ModelState.IsValid)
            {
                blogRollModel.BlogRollId = id;
                var blog = BlogRepository.GetBlogByUserName(User.Identity.Name);
                blogRollModel.BlogId = blog.BlogId;
                BloggRollRepository.UpdateBlogRoll(blogRollModel);
                ViewBag.Saved = "Länken är nu uppdaterad";
                return View(blogRollModel);
            }
            return EditLink(id);
        }

        [HttpGet]
        public ActionResult DeleteLink(int id)
        {
            var bloggRollModel = BloggRollRepository.GetBlogRollById(id);
            if (bloggRollModel == null)
                return HttpNotFound();
            return View(bloggRollModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteLink(int id, BlogRollModel blogRollModel)
        {
            BloggRollRepository.DeleteBloggRoll(id);
            return RedirectToAction("BlogRoll");
        }

        [HttpGet]
        public ActionResult BlogSettings()
        {
            var blog = BlogRepository.GetBlogByUserName(User.Identity.Name);
            return View(blog);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult BlogSettings(BlogDetailsModel blogDetails)
        {
            if (ModelState.IsValid)
            {
                BlogRepository.UpdateBlog(blogDetails);
                return View(blogDetails);
            }

            return BlogSettings();
        }

        [HttpGet]
        public ActionResult UserSettings()
        {
            int userId = (int)Session["User"];
            var user = UserRepository.GetUserOnUserId(userId);

            if (user != null)
            {
                return View(user);
            }
            return HttpNotFound();

        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult UserSettings(UserModel userModel)
        {
            if (ModelState.IsValid)
            {
                UserRepository.UpdateUser(userModel);
                return View(userModel);
            }

            return UserSettings();
        }

        [HttpGet]
        public ActionResult DeleteComment(int id)
        {
            var comment = CommentRepository.GetCommentById(id);
            if (comment == null)
                return HttpNotFound();
            return View(comment);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteComment(int id, CommentModel commentModel)
        {
            CommentRepository.DeleteComment(id);
            return RedirectToAction("EditPost", new { id = commentModel.PostId });
        }

    }
}